<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-btn" @click="toggleCollapse"><i :class="!isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"></i></div>
        <el-menu :default-active="$route.path" class="el-menu-vertical-demo" background-color="#313743" text-color="#fff"
          active-text-color="#359bff" unique-opened :collapse="isCollapse" :collapse-transition="false" router>
            <el-submenu :index="'/' + menu.path" :key="menu.id" v-for="menu,index in menuList">
              <template slot="title">
                <i :class="iconList[index]"></i>
                <span>{{ menu.authName }}</span>
              </template>
              <el-menu-item :index="'/' + item.path" v-for="item in menu.children" :key="item.id">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ item.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import menu from '@/api/menu'

export default {
  name: 'MyHome',
  data () {
    return {
      menuList: [],
      iconList: ['iconfont icon-user', 'iconfont icon-tijikongjian',
        'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao'],
      isCollapse: false
    }
  },
  components: {
  },
  methods: {
    // 1.退出登录
    logout () {
      // 清除session
      sessionStorage.clear()
      // 跳转到登录
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    // 2.切换菜单的折叠展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
  },
  async created () {
    // 获取左侧菜单数据
    const { data: res } = await menu.getMenus()
    if (res.meta.status === 200) {
      this.menuList = res.data
    } else {
      return this.$message.success(res.meta.msg)
    }
  },
  mounted () {
  }
}
</script>

<style lang='less' scoped>
.el-container {
  height: 100%;
}

.el-aside {
  background-color: #313743;
}

.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 0;
  background-color: #2e3436;
  color: #fff;
  font-size: 18px;

  >div {
    display: flex;
    align-items: center;

    span {
      margin-left: 20px;
    }
  }
}

.el-main {
  padding: 20px;
  overflow: scroll-y;
  background-color: #eaedf1;
}

.el-menu {
  border-right: none;
}

.iconfont {
  margin-right: 10px;
}

.toggle-btn {
  background-color: #475163;
  text-align: center;
  font-size: 16px;
  line-height: 24px;
  color: #fff;
  cursor: pointer;
}
</style>
